const User = require('../db/model/user');
const cryptoHelper = require('../helper/crypto');

const add = async (username, email, password) => {
    // Hash the password for storage
    let passwordHash = await cryptoHelper.hashString(password);

    // Add the user.
    let user = await User.query().insert({
        username,
        email,
        password: passwordHash
    });

    // return the user object
    return user;
};

const get = async (email) => {
    // Hash the password for storage
    // Add the user.
    let user = await User.query().findOne({email});
    
    // return the user object
    return user;
};

const generateToken = async(userData) => {
    return cryptoHelper.generateWebToken(userData);
};

module.exports = {
    add: add,
    get: get,
    generateToken
};